package gemini.castor.ui.client.page.content.office.events.edit;

import gemini.basic.model.Distributor;
import gemini.basic.model.Event;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.RootEvent;
import gemini.castor.ui.client.page.content.office.events.EventsEvent;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.PlaceConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.inject.Inject;

public class EventsEditPresenter extends
		CastorWidgetPresenter<EventsEditPresenter.Display> {

	@Inject
	public EventsEditPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {
		HasClickHandlers getBackClickHandlers();

		HasClickHandlers getSubmitClickHandlers();

		Event getEvent();

		void setEvent(Event event);

		boolean isValidate();

		void loadForm();

		void initCustom();
	}
	
	private String distributorCode;
	
	private Event event;

	@Override
	public void setInputObject(Object ob) {
		if (ob instanceof Event){
			event = (Event)ob;
		}else{
			distributorCode = (String) ob;
			event = null;
		}
	}

	public static final Place PLACE = new Place(PlaceConstants.PLACE_EVENTS);

	@Override
	protected void onBind() {
		display.setEvent(event);
		display.initCustom();
		display.loadForm();
		
		registerHandler(display.getBackClickHandlers().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						localEventBus.fireEvent(new EventsEvent(
								EventsEvent.ForwardType.BACK_TO_LIST));
					}
				}));

		registerHandler(display.getSubmitClickHandlers().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						if (display.isValidate()) {
							Event newEvent = display.getEvent();
							Distributor distributor = new Distributor();
							distributor.setCode(distributorCode);
							newEvent.setDistributor(distributor);
							eventBus.fireEvent(new RootEvent(
									RootEvent.ForwardType.MASK));
							ServiceCatalog.getEventService().saveOrUpdateEvent(
									newEvent,
									new AbstractAsyncCallback<Event>() {
										@Override
										public void onSuccess(Event result) {
											eventBus.fireEvent(new RootEvent(
													RootEvent.ForwardType.UN_MASK));
											confirmBackToList();
										}
									});
						}
					}
				}));

	}

	private void confirmBackToList() {
		String message = "Save or update event successfull!";

		GuiUtils.info(message);

		localEventBus.fireEvent(new EventsEvent(
				EventsEvent.ForwardType.BACK_TO_LIST));
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	public void onUnbind() {
		event = null;
	}
}
